'use strict';//最受关注电影
const Controller = require('egg').Controller;
class most_filmController extends Controller {
    async index() { //模糊搜索跟分页查询
        const { ctx, app } = this;
        const { keyname, page = 1, number = 5 } = ctx.request.query
        const offset = (page - 1) * number
        let data, count
        if (keyname && page) {
            data = await app.mysql.query(`select id,movie_name,time_r from most_film where id like '%${keyname}%' limit ${offset},${number}`)
            count = await app.mysql.query(`select count(*) as count from most_film where id like '%${keyname}%'`)
        } else if (keyname) {
            data = await app.mysql.query(`select id,movie_name,time_r from most_film where id ='%${keyname}%' or id  like '%${keyname}%' limit ${offset},${number}`)
            count = await app.mysql.query(`select count(*) as count from id where id like '%${keyname}%' or id like '%${keyname}%'`)
        } else {
            data = await app.mysql.query(`select id,movie_name,time_r from most_film limit ${offset},${number}`)
            count = await app.mysql.query(`select count(*) as count from most_film`)
        }
        ctx.body = {
            code: 0,
            msg: "查找成功",
            data,
            count: count
        }
    }
    async create() {
        const { ctx, app } = this;
        const {  movie_name, time_r } = ctx.request.body;
        const result = await app.mysql.insert('most_film', { movie_name, time_r: Date.now() })
        if (result) {
            ctx.body = {
                code: 0,
                msg: "添加成功",
                data: result
            };
        } else {
            ctx.body = {
                code: 406,
                msg: "添加失败"
            };
        }
    }
    async update() {
        const { ctx, app } = this;
        const {  movie_name, time_r } = ctx.request.body;
        const { id } = ctx.params;
        const row = {

            
            movie_name,
            time_r: Date.now()

        }
        const options = {
            where: {
                id: +id
            }
        };
        const result = await app.mysql.update('most_film', row, options)
        if (id, content, time_r) {
            ctx.body = {
                code: 0,
                msg: "修改成功",
                data: result
            };
        } else {
            ctx.body = {
                code: 408,
                msg: "修改失败",
            };
        }
    }

    async destroy() {
        const { ctx, app } = this;
        const { id } = ctx.params;
        const result = await app.mysql.delete(`most_film`, { id: +id })
        if (result) {
            ctx.body = {
                code: 0,
                msg: "删除成功",
                data: result
            };
        } else {
            ctx.body = {
                code: 407,
                msg: "删除失败"
            };
        }
    }
}
module.exports = most_filmController;
